<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      size="small"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="采购仓库" prop="wareId">
        <el-select
          v-model="dataForm.wareId"
          placeholder="请选择采购仓库"
          clearable
        >
          <el-option
            :label="w.name"
            :value="w.id"
            v-for="w in wareList"
            :key="w.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="采购商品id" prop="skuId">
        <el-input v-model="dataForm.skuId" placeholder="采购商品id"></el-input>
      </el-form-item>

      <el-form-item label="采购数量" prop="skuNum">
        <el-input-number
          v-model="dataForm.skuNum"
          controls-position="right"
          :min="0"
          label="采购数量"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { wareInfoList, warePurchaseDeatailSaveAndUpdate } from "@/api/ware";
export default {
  data() {
    return {
      visible: false,
      wareList: [],
      dataForm: {
        id: 0,
        skuId: "",
        skuNum: 1,
        skuPrice: "",
        wareId: "",
        status: 0,
      },
      dataRule: {
        skuId: [
          { required: true, message: "采购商品不能为空", trigger: "blur" },
        ],
        skuNum: [
          { required: true, message: "采购数量不能为空", trigger: "blur" },
        ],
        wareId: [
          { required: true, message: "仓库不能为空", trigger: "blur" },
        ],
         purchaseId: [
          { required: true, message: "采购单不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getWares();
  },
  methods: {
    getWares() {
      wareInfoList().then((res) => {
        if (res && res.code === 0) {
          this.wareList = res.data;
        } else {
          this.wareList = [];
        }
      });
    },
    init(data) {
      this.dataForm.id = 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (data) {
          this.dataForm.id = data.id;
          this.dataForm.skuId = data.skuId;
          this.dataForm.skuNum = data.skuNum;
          this.dataForm.skuPrice = data.skuPrice;
          this.dataForm.wareId = data.wareId;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          warePurchaseDeatailSaveAndUpdate({
            id: this.dataForm.id || "",
            skuId: this.dataForm.skuId,
            skuNum: this.dataForm.skuNum,
            skuPrice: this.dataForm.skuPrice,
            wareId: this.dataForm.wareId,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
